const { DataTypes } = require('sequelize');
const { sequelize } = require('../utils/database');

const Badge = sequelize.define('Badge', {
  id: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    autoIncrement: true
  },
  user_id: {
    type: DataTypes.INTEGER,
    allowNull: false
  },
  activity_id: {
    type: DataTypes.INTEGER,
    allowNull: false
  },
  token_id: {
    type: DataTypes.STRING(255),
    allowNull: true,
    unique: true
  },
  contract_address: {
    type: DataTypes.STRING(255),
    allowNull: true
  },
  transaction_hash: {
    type: DataTypes.STRING(255),
    allowNull: true
  },
  metadata_uri: {
    type: DataTypes.STRING(500),
    allowNull: true
  },
  badge_name: {
    type: DataTypes.STRING(255),
    allowNull: false
  },
  badge_description: {
    type: DataTypes.TEXT,
    allowNull: true
  },
  badge_image_url: {
    type: DataTypes.STRING(500),
    allowNull: true
  },
  rarity: {
    type: DataTypes.ENUM('common', 'rare', 'epic', 'legendary'),
    defaultValue: 'common'
  },
  attributes: {
    type: DataTypes.JSON,
    allowNull: true
  },
  minted_at: {
    type: DataTypes.DATE,
    allowNull: true
  },
  status: {
    type: DataTypes.ENUM('pending', 'minting', 'minted', 'failed'),
    defaultValue: 'pending'
  }
}, {
  tableName: 'nft_badges',
  indexes: [
    {
      fields: ['user_id']
    },
    {
      fields: ['activity_id']
    },
    {
      fields: ['token_id']
    },
    {
      fields: ['status']
    }
  ]
});

module.exports = Badge; 